//
//  MTAllopatryWarningView.swift
//  MT
//
//  Created by ooops on 2020/5/29.
//  Copyright © 2020 张强. All rights reserved.
//

import UIKit
import MTCategory

protocol MTCancelTaskWarningViewDelegate: NSObjectProtocol {
    func cancelPartyTask(view: MTCancelTaskWarningView)
}

class MTCancelTaskWarningView: UIView {
    
    weak var delegate: MTCancelTaskWarningViewDelegate?
    
    convenience init(title: String, content: String) {
        self.init()
        titleLabel.text = title
        subTitleLabel.text = content
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        makeConstraint()
        backgroundColor = UIColor(hex: 0x000000, alpha: 0.5)
    }
    
    lazy var centerView: UIView = {
        let v = UIView()
        v.backgroundColor = .white
        v.layer.cornerRadius = 8
        return v
    }()
    
    lazy var titleLabel: UILabel = {
        let l = UILabel()
        l.font = PingFangSCSemiboldFont(16)
        l.textColor = UIColor.text.x1d1a1a
        l.text = "取消须知"
        return l
    }()
    
    lazy var subTitleLabel: UILabel = {
        let l = UILabel()
        l.font = PingFangSCRegularFont(14)
        l.textColor = UIColor.text.x1d1a1a
        l.numberOfLines = 0
        
        let paraStyle: NSMutableParagraphStyle = NSMutableParagraphStyle()
        paraStyle.lineSpacing = 8
        
        let attributeAll = [NSAttributedString.Key.font: PingFangSCRegularFont(14),
                                    NSAttributedString.Key.foregroundColor: UIColor.text.x1d1a1a,
                                    NSAttributedString.Key.paragraphStyle : paraStyle]
        let text = "1.聚会任务开启1小时前不能取消\n2.任务群中只剩一人,可进行取消\n3.取消3次/月以上,该月限制发布任务."
        l.attributedText = NSAttributedString(string: text, attributes: attributeAll)
        return l
    }()
    
    lazy var cancelButton: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setTitle("确认取消", for: .normal)
        btn.backgroundColor = .white
        btn.addTarget(self, action: #selector(MTCancelTaskWarningView.cancel), for: .touchUpInside)
        btn.setTitleColor(UIColor.text.x1d1a1a, for: .normal)
        btn.titleLabel?.font = PingFangSCSemiboldFont(16)
        btn.layer.cornerRadius = 19.5
        btn.layer.masksToBounds = true
        return btn
    }()
    
    lazy var uncancelButton: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setTitle("不取消了", for: .normal)
        btn.backgroundColor = .white
        btn.addTarget(self, action: #selector(MTCancelTaskWarningView.uncancel), for: .touchUpInside)
        btn.setTitleColor(UIColor.init(red: 60, green: 163, blue: 48), for: .normal)
        btn.titleLabel?.font = PingFangSCSemiboldFont(16)
        btn.layer.cornerRadius = 19.5
        btn.layer.masksToBounds = true
        return btn
    }()
    
    lazy var lineViewH: UIView = {
        let v = UIView()
        v.backgroundColor = UIColor.seprateLine.x707070
        return v
    }()
    
    lazy var lineViewV: UIView = {
        let v = UIView()
        v.backgroundColor = UIColor.seprateLine.x707070
        return v
    }()
    
    @objc private func uncancel() {
        self.removeFromSuperview()
    }
    
    @objc private func cancel() {
        delegate?.cancelPartyTask(view: self)
    }
    
    private func setupUI() {
        addSubview(centerView)
        centerView.addSubview(titleLabel)
        centerView.addSubview(subTitleLabel)
        centerView.addSubview(cancelButton)
        centerView.addSubview(uncancelButton)
        centerView.addSubview(lineViewH)
        centerView.addSubview(lineViewV)
    }
    
    private func makeConstraint() {
        centerView.snp.makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 325, height: 210))
            make.center.equalToSuperview()
        }
        
        titleLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(20)
        }
        
        subTitleLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(titleLabel.snp.bottom).offset(16)
            make.left.equalToSuperview().offset(24)
            make.right.equalToSuperview().offset(-24)
        }
        
        cancelButton.snp.makeConstraints { (make) in
            make.bottom.left.equalToSuperview()
            make.height.equalTo(50)
            make.width.equalToSuperview().multipliedBy(0.5)
        }
        
        uncancelButton.snp.makeConstraints { (make) in
            make.bottom.right.equalToSuperview()
            make.height.equalTo(50)
            make.width.equalToSuperview().multipliedBy(0.5)
        }
        
        lineViewV.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.bottom.equalTo(uncancelButton.snp.top)
            make.height.equalTo(1)
        }
        
        lineViewH.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(lineViewV.snp.bottom)
            make.bottom.equalToSuperview()
            make.width.equalTo(1)
        }
        
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }


}
